<?php

namespace App\Admin\Forms;

use App\Models\WxCaptchaImg;
use App\Wen\Utils\Settings;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class CaptchaForm extends Form implements LazyRenderable
{
    use LazyWidget;

    public function handle(array $input)
    {
        if(WxCaptchaImg::where('status', 1)->count() <= 0){
            return $this->response()->error('人机验证码库一张图片都没有，无法开启人机验证');
        }

        Settings::set('captcha_scenes', $input['captcha_scenes'], true);
        Settings::set('captcha_scenes_h5', $input['captcha_scenes_h5'], true);
        Settings::set('captcha_scenes_android_app', $input['captcha_scenes_android_app'], true);
        Settings::set('captcha_scenes_ios_app', $input['captcha_scenes_ios_app'], true);

        return $this->response()
            ->success('配置成功.')
            ->refresh();
    }

    public function form()
    {
        $this->html(function () {
            return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">需要人机验证的的场景</div>';
        }, '');
        $this->html(function () {
            return '<div>---------------------------------------------------------------------------------</div>';
        }, '<b>小程序</b>');
        $this->checkbox('captcha_scenes', '人机验证场景')->options(['phone_code' => '短信验证码前'])->default(Settings::get('captcha_scenes', []));
        $this->html(function () {
            return '<div>---------------------------------------------------------------------------------</div>';
        }, '<b>H5</b>');
        $this->checkbox('captcha_scenes_h5', '人机验证场景')->options(['phone_code' => '短信验证码前'])->default(Settings::get('captcha_scenes_h5', []));
        $this->html(function () {
            return '<div>---------------------------------------------------------------------------------</div>';
        }, '<b>安卓App</b>');
        $this->checkbox('captcha_scenes_android_app', '人机验证场景')->options(['phone_code' => '短信验证码前'])->default(Settings::get('captcha_scenes_android_app', []));
        $this->html(function () {
            return '<div>---------------------------------------------------------------------------------</div>';
        }, '<b>IOS App</b>');
        $this->checkbox('captcha_scenes_ios_app', '人机验证场景')->options(['phone_code' => '短信验证码前'])->default(Settings::get('captcha_scenes_ios_app', []));
    }
}
